Page({
  data: {
    activeTab: 0, // 当前选中的选项卡索引
    orderStatus: [
      { text: '全部', status: 'all' },
      { text: '待支付', status: 'unpaid' },
      { text: '待发货', status: 'unshipped' },
      { text: '待收货', status: 'shipping' },
      { text: '已完成', status: 'completed' },
      { text: '已取消', status: 'cancelled' }
    ],
    orderList: {
      all: [
        { id: 1001, status: 'unpaid', time: '2025-06-08 10:30', amount: 299, products: [
          { name: '时尚运动鞋', image: '/images/product.jpg', price: 299, count: 1 }
        ]},
        { id: 1002, status: 'unshipped', time: '2025-06-07 15:45', amount: 899, products: [
          { name: '智能手表', image: '/images/product.jpg', price: 899, count: 1 }
        ]},
        { id: 1003, status: 'shipping', time: '2025-06-06 09:15', amount: 1198, products: [
          { name: '无线耳机', image: '/images/product.jpg', price: 599, count: 2 }
        ]},
        { id: 1004, status: 'completed', time: '2025-06-05 16:20', amount: 4299, products: [
          { name: '轻薄笔记本电脑', image: '/images/product.jpg', price: 4299, count: 1 }
        ]},
        { id: 1005, status: 'cancelled', time: '2025-06-04 11:05', amount: 399, products: [
          { name: '蓝牙音箱', image: '/images/product.jpg', price: 399, count: 1 }
        ]}
      ],
      unpaid: [
        { id: 1001, status: 'unpaid', time: '2025-06-08 10:30', amount: 299, products: [
          { name: '时尚运动鞋', image: '/images/product.jpg', price: 299, count: 1 }
        ]}
      ],
      unshipped: [
        { id: 1002, status: 'unshipped', time: '2025-06-07 15:45', amount: 899, products: [
          { name: '智能手表', image: '/images/product.jpg', price: 899, count: 1 }
        ]}
      ],
      shipping: [
        { id: 1003, status: 'shipping', time: '2025-06-06 09:15', amount: 1198, products: [
          { name: '无线耳机', image: '/images/product.jpg', price: 599, count: 2 }
        ]}
      ],
      completed: [
        { id: 1004, status: 'completed', time: '2025-06-05 16:20', amount: 4299, products: [
          { name: '轻薄笔记本电脑', image: '/images/product.jpg', price: 4299, count: 1 }
        ]}
      ],
      cancelled: [
        { id: 1005, status: 'cancelled', time: '2025-06-04 11:05', amount: 399, products: [
          { name: '蓝牙音箱', image: '/images/product.jpg', price: 399, count: 1 }
        ]}
      ]
    }
  },

  onLoad(options) {
    const {id} = options
    // 1：待付款(1) 2：待发货(2) 3：待收货(3) 4：待评价(4)  
    console.log(id)
    let activeTab = Number(id)
    console.log(activeTab)
    this.setData({
      activeTab
    })
    // 模拟加载订单数据
    this.loadOrders();
  },

  // 加载订单数据
  loadOrders() {
    // 实际项目中应从服务器获取数据
    // 这里使用模拟数据
  },

  // 选项卡切换事件
  onTabChange(event) {
    this.setData({
      activeTab: event.detail
    });
  },

  // 支付订单
  onPayOrder(orderId) {
    console.log('支付订单:', orderId);
    wx.showModal({
      title: '确认支付',
      content: `是否确认支付订单 ${orderId}？`,
      success: (res) => {
        if (res.confirm) {
          // 调用支付API
          wx.showToast({
            title: '支付成功',
            icon: 'success'
          });
          // 更新订单状态
          this.updateOrderStatus(orderId, 'unshipped');
        }
      }
    });
  },

  // 取消订单
  onCancelOrder(orderId) {
    console.log('取消订单:', orderId);
    wx.showModal({
      title: '取消订单',
      content: '是否确认取消该订单？',
      success: (res) => {
        if (res.confirm) {
          wx.showToast({
            title: '订单已取消',
            icon: 'success'
          });
          // 更新订单状态
          this.updateOrderStatus(orderId, 'cancelled');
        }
      }
    });
  },

  // 确认收货
  onConfirmReceipt(orderId) {
    console.log('确认收货:', orderId);
    wx.showModal({
      title: '确认收货',
      content: '是否确认收到商品？',
      success: (res) => {
        if (res.confirm) {
          wx.showToast({
            title: '收货成功',
            icon: 'success'
          });
          // 更新订单状态
          this.updateOrderStatus(orderId, 'completed');
        }
      }
    });
  },

  // 更新订单状态（模拟）
  updateOrderStatus(orderId, newStatus) {
    // 实际项目中应调用API更新订单状态
    // 这里仅更新本地数据用于演示
    const allOrders = this.data.orderList.all;
    const index = allOrders.findIndex(order => order.id === orderId);
    
    if (index !== -1) {
      allOrders[index].status = newStatus;
      this.setData({
        'orderList.all': allOrders
      });
      
      // 更新各状态分类的订单列表
      this.classifyOrders();
    }
  },

  // 分类订单（实际项目中应由服务器返回分类后的数据）
  classifyOrders() {
    const allOrders = this.data.orderList.all;
    const classifiedOrders = {
      all: allOrders,
      unpaid: allOrders.filter(order => order.status === 'unpaid'),
      unshipped: allOrders.filter(order => order.status === 'unshipped'),
      shipping: allOrders.filter(order => order.status === 'shipping'),
      completed: allOrders.filter(order => order.status === 'completed'),
      cancelled: allOrders.filter(order => order.status === 'cancelled')
    };
    
    this.setData({
      orderList: classifiedOrders
    });
    
  },

  // 查看物流
  goLogistics() {
    wx.navigateTo({
      url: '/pages/logistics/logistics',
    })
  }
});    